package org.thss.petri.gef.editparts.policies;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.thss.petri.gef.commands.CreateNodeCommand;
import org.thss.petri.gef.commands.MoveNodeCommand;
import org.thss.petri.model.Node;
import org.thss.petri.model.PetriNet;

public class NodeLayoutEditPolicy extends XYLayoutEditPolicy {

	@Override
	protected Command getCreateCommand(CreateRequest request) {
		Object newObject = request.getNewObject();
		if (!(newObject instanceof Node)) {
			return null;
		}
		Node node = (Node) newObject;
		CreateNodeCommand command = new CreateNodeCommand();
		Object hostModel = getHost().getModel();
		if (hostModel instanceof PetriNet) {
			command.setNet((PetriNet) hostModel);
		}
		command.setPosition(request.getLocation());
		command.setSize(request.getSize());
		command.setNode(node);
		return command;
	}

	@Override
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		Object model = child.getModel();
		if (!(model instanceof Node)) {
			return null;
		}
		MoveNodeCommand command = new MoveNodeCommand();
		command.setNode((Node) model);
		command.setConstraint((Rectangle) constraint);
		return command;
	}

}
